package com.zhida84.Servlet;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.Md5Crypt;

import com.zhida84.Convert.Base64CodeO;
import com.zhida84.Convert.JMMD5;
import com.zhida84.Dao.UserDao;
import com.zhida84.Entity.UserTab;
import com.zhida84.Impl.UserImpl;

/**
 * Servlet implementation class loginServlet
 */
public class registServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public registServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			response.setContentType("text/html;charset=GBK");
			request.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			String userPhone = request.getParameter("userPhone");
			String pwd = request.getParameter("userPwd");
			
			UserTab userTB = new UserTab();
			userTB.setUserPhone(Base64CodeO.encoded(userPhone));
			userTB.setUserPwd(JMMD5.MD5(pwd));
			userTB.setUserReName(userPhone);
			UserDao userDao = new UserImpl();
			if (userDao.valUserTab(userTB).next()) {
				out.print("<script>alert('账号已存在!');window.location.href='regist.jsp';</script>");
				return;
			}
			if (userDao.RegistUser(userTB) > 0) {
				out.print("<script>alert('注册成功!');window.location.href='personal.html';</script>");
			} else {
				response.sendRedirect("error/RegistError.html");
			}
			out.close();
		} catch (Exception e) {

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
